/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pgpproj.pgpframework.crypto.engine;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import pgpproj.pgpframework.crypto.algorithm.CipherAlgorithmEnum;
import sun.misc.BASE64Encoder;

/**
 *
 * @author root
 */
public class SymetricCipherEngine {

    public byte[] encryptData(byte[] byteDataToEncrypt, Key secretKey, String Algorithm) {
        byte[] byteCipherText = new byte[200];

        try {
            Cipher aesCipher = Cipher.getInstance(Algorithm);

            if (Algorithm.equals("AES")) {
                aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, aesCipher.getParameters());
            } else if (Algorithm.equals("RSA/ECB/PKCS1Padding")) {
                aesCipher.init(Cipher.ENCRYPT_MODE, secretKey);
            }

            byteCipherText = aesCipher.doFinal(byteDataToEncrypt);

        } catch (NoSuchAlgorithmException noSuchAlgo) {
            System.out.println(" Algoritmo nao existe " + noSuchAlgo);
        } catch (NoSuchPaddingException noSuchPad) {
            System.out.println(" Paffing nao existe " + noSuchPad);
        } catch (InvalidKeyException invalidKey) {
            System.out.println(" chave invalida " + invalidKey);
        } catch (BadPaddingException badPadding) {
            System.out.println(" padding ruim " + badPadding);
        } catch (IllegalBlockSizeException illegalBlockSize) {
            System.out.println(" tamanho de bloco invalido " + illegalBlockSize);
            illegalBlockSize.printStackTrace();
        } catch (Exception exp) {
            exp.printStackTrace();
        }

        return byteCipherText;
    }

    public byte[] decryptData(byte[] byteCipherText, Key secretKey, String Algorithm) {
        byte[] byteDecryptedText = new byte[200];

        try {
            Cipher aesCipher = Cipher.getInstance(Algorithm);
            if (Algorithm.equals("AES")) {
                aesCipher.init(Cipher.DECRYPT_MODE, secretKey, aesCipher.getParameters());
            } else if (Algorithm.equals("RSA/ECB/PKCS1Padding")) {
                aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
            }

            byteDecryptedText = aesCipher.doFinal(byteCipherText);
        } catch (NoSuchAlgorithmException noSuchAlgo) {
            System.out.println(" Algoritmo nao existe " + noSuchAlgo);
        } catch (NoSuchPaddingException noSuchPad) {
            System.out.println(" Paffing nao existe " + noSuchPad);
        } catch (InvalidKeyException invalidKey) {
            System.out.println(" chave invalida " + invalidKey);
            invalidKey.printStackTrace();
        } catch (BadPaddingException badPadding) {
            System.out.println(" padding ruim  " + badPadding);
            badPadding.printStackTrace();
        } catch (IllegalBlockSizeException illegalBlockSize) {
            System.out.println(" Itamanho de bloco invalido " + illegalBlockSize);
            illegalBlockSize.printStackTrace();
        } catch (InvalidAlgorithmParameterException invalidParam) {
            System.out.println(" parametro invalido " + invalidParam);
        }

        return byteDecryptedText;

    }
}